package com.bls.cate.adapter;

import android.support.annotation.Nullable;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.ScreenUtils;
import com.blankj.utilcode.util.SizeUtils;
import com.bls.cate.R;
import com.bls.cate.model.ArticleListRes;
import com.bls.cate.utils.FrescoImageLoader;
import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.facebook.drawee.view.SimpleDraweeView;

import java.util.List;

/**
 * 　　  () 　　　  ()
 * 　　  ( ) 　　　( )
 * 　　  ( ) 　　　( )
 * 　　┏┛┻━━━┛┻┓
 * 　　┃　　　━　　　┃
 * 　　┃　┳┛　┗┳　┃
 * 　　┃　　　┻　　　┃
 * 　　┗━┓　　　┏━┛
 * 　　　　┃　　　┃
 * 　　　　┃　　　┗━━━┓
 * 　　　　┃　　　　　　　┣┓
 * 　　　　┃　　　　　　　┏┛
 * 　　　　┗┓┓┏━┳┓┏┛
 * 　　　　　┃┫┫　┃┫┫
 * 　　　　　┗┻┛　┗┻┛
 * ——————————————————
 * Created by BinglingSu on 2018/2/7.
 */

public class ArticleListAdapter extends BaseQuickAdapter<ArticleListRes.ResultListDataBean, BaseViewHolder> {

    private static final int Screen_Width = ScreenUtils.getScreenWidth();

    public ArticleListAdapter(@Nullable List<ArticleListRes.ResultListDataBean> data) {
        super(R.layout.item_article_list, data);
    }

    @Override
    protected void convert(BaseViewHolder helper, ArticleListRes.ResultListDataBean item) {
        RelativeLayout rlRoot = helper.getView(R.id.rl_root);
        ImageView ivImage = helper.getView(R.id.iv_image);
        SimpleDraweeView ivHeader = helper.getView(R.id.iv_header);
        TextView tvTitle = helper.getView(R.id.tv_title);
        TextView tvName = helper.getView(R.id.tv_name);
        StaggeredGridLayoutManager.LayoutParams rlRootParams = (StaggeredGridLayoutManager.LayoutParams) rlRoot.getLayoutParams();
        rlRootParams.width = Screen_Width / 2;
        rlRootParams.leftMargin = 10;
        rlRootParams.rightMargin = 10;
        rlRoot.setLayoutParams(rlRootParams);
        try {
            if (null != item.getArticleImageList() && !item.getArticleImageList().isEmpty() && item.getArticleImageList().size() > 0) {
                double ratio = (double) item.getArticleImageList().get(0).getWidth() / (double) item.getArticleImageList().get(0).getHeight();
                int ivImageWidth = Screen_Width / 2 - 20;
                int ivImageHeight = (int) (ivImageWidth / ratio);
                RelativeLayout.LayoutParams ivImageParams = (RelativeLayout.LayoutParams) ivImage.getLayoutParams();
                ivImageParams.width = ivImageWidth;
                ivImageParams.height = ivImageHeight;
                ivImage.setScaleType(ImageView.ScaleType.FIT_START);
                Glide.with(mContext).load(item.getArticleImageList().get(0).getImagePath()).crossFade().override(ivImageWidth, ivImageHeight).into(ivImage);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        FrescoImageLoader.showImage(ivHeader, item.getHeaderImgSmall());
        tvTitle.setText(item.getArticleTitle());
        tvName.setText(item.getNickName());
    }
}
